package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.efp.nls.common.ConsoleConstant;
import com.fdb.efp.esb.service.bo.req.basicfn.AccountingReqBean;
import com.fdb.efp.esb.service.bo.resp.basicfn.AccountingRespBean;
import com.fdb.efp.esb.service.facade.basicfn.AccountingService;
import com.fdb.efp.flow.common.vo.PageApproveVO;
import com.fdb.efp.flow.service.facade.BizEventInfoService;
import com.fdb.efp.nls.service.facade.AccLoanService;
import com.fdb.efp.nls.service.vo.AccLoanVO;
import com.fdb.efp.nls.service.facade.ColltWriteOffAppApprService;
import com.fdb.efp.nls.service.facade.ColltWriteOffAppService;
import com.fdb.efp.nls.service.vo.ColltWriteOffAppVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 核销服务-流程
 * @author xieziwei
 * @version 0.1
 * @return ${return}
 * @date 2020/3/11 17:31
 */
@Service("colltWriteOffAppApprService")
public class ColltWriteOffAppApprServiceImpl implements ColltWriteOffAppApprService, FrameworkService {

    private static Logger logger = LoggerFactory.getLogger(LmtTerminateAppApprServiceImpl.class);

    @Autowired
    @Qualifier("bizEventInfoService")
    private BizEventInfoService bizEventInfoService;

    @Autowired
    @Qualifier("colltWriteOffAppService")
    private ColltWriteOffAppService colltWriteOffAppService;

    @Autowired
    @Qualifier("accountingService")
    private AccountingService accountingService;

    @Override
    public void pass(String bizSerno, PageApproveVO pageApproveVo) {
        logger.info("核销申请通过流水号" + bizSerno);
        int num = 0;
        try {
            if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
                ColltWriteOffAppVO colltWriteOffAppVO = new ColltWriteOffAppVO();
                colltWriteOffAppVO.setBizSerno(bizSerno);
                colltWriteOffAppVO.setAprvDate(TimeUtil.getCurrentDate());
                colltWriteOffAppVO.setAprvUserCode(pageApproveVo.getAprvUserId());
                colltWriteOffAppVO.setAprvStatus(ConsoleConstant.APRV_STATUS_03);
                colltWriteOffAppVO.setAprvComment(pageApproveVo.getAprvComment());
                colltWriteOffAppVO.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                colltWriteOffAppVO.setLastUpdateUser(pageApproveVo.getAprvUserId());
                num = colltWriteOffAppService.updateByCondition(colltWriteOffAppVO);
                if (num < 1) {
                    logger.info("更新核销审批信息失败");
                }
                List<ColltWriteOffAppVO> colltWriteOffAppVOS = colltWriteOffAppService.queryByPk(colltWriteOffAppVO);
                for (ColltWriteOffAppVO item : colltWriteOffAppVOS){
                    if (!excuteAccounting(item)){
                        item.setAprvStatus(ConsoleConstant.APRV_STATUS_05);
                        item.setAprvComment("【系统提示】 调用接口失败，系统已打回您的申请！");
                        item.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                        item.setLastUpdateUser("admin");
                        colltWriteOffAppService.updateByCondition(item);
                    }else{
                        //更新借据状态=核销状态(9)
                        AccLoanVO accLoanVO = new AccLoanVO();
                        accLoanVO.setBillNo(item.getLoanNo());
                        accLoanVO.setAccountStatus("9");//核销
                        accLoanVO.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                        int i = accLoanService.updateByPk(accLoanVO);
                        if (i < 1){
                            logger.error("更新借据失败。。。。");
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info(">>>>>>>核销申请通过流程后处理结束<<<<<<");

    }

    @Override
    public void refuse(String bizSerno, PageApproveVO pageApproveVo) {
        logger.info("核销申请拒绝流水号" + bizSerno);
        int num = 0;
        try {
            if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
                ColltWriteOffAppVO colltWriteOffAppVO = new ColltWriteOffAppVO();
                colltWriteOffAppVO.setBizSerno(bizSerno);
                colltWriteOffAppVO.setAprvDate(TimeUtil.getCurrentDate());
                colltWriteOffAppVO.setAprvUserCode(pageApproveVo.getAprvUserId());
                colltWriteOffAppVO.setAprvStatus(ConsoleConstant.APRV_STATUS_04);
                colltWriteOffAppVO.setAprvComment(pageApproveVo.getAprvComment());
                colltWriteOffAppVO.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                colltWriteOffAppVO.setLastUpdateUser(pageApproveVo.getAprvUserId());
                num = colltWriteOffAppService.updateByCondition(colltWriteOffAppVO);
                if (num < 1) {
                    logger.info("更新核销审批信息失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info(">>>>>>>核销申请拒绝流程后处理结束<<<<<<");
    }


    @Override
    public void repulse(String bizSerno, PageApproveVO pageApproveVo) {
        int num = 0;
        try {
            if (bizEventInfoService.isFirstNode(bizSerno)) {
                logger.info("核销申请打回流水号" + bizSerno);
                if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
                    ColltWriteOffAppVO colltWriteOffAppVO = new ColltWriteOffAppVO();
                    colltWriteOffAppVO.setBizSerno(bizSerno);
                    colltWriteOffAppVO.setAprvDate(TimeUtil.getCurrentDate());
                    colltWriteOffAppVO.setAprvUserCode(pageApproveVo.getAprvUserId());
                    colltWriteOffAppVO.setAprvStatus(ConsoleConstant.APRV_STATUS_05);
                    colltWriteOffAppVO.setAprvComment(pageApproveVo.getAprvComment());
                    colltWriteOffAppVO.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                    colltWriteOffAppVO.setLastUpdateUser(pageApproveVo.getAprvUserId());
                    num = colltWriteOffAppService.updateByCondition(colltWriteOffAppVO);
                    if (num < 1) {
                        logger.info("更新核销审批信息失败");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info(">>>>>>>核销申请打回流程后处理结束<<<<<<");
    }

    private boolean excuteAccounting(ColltWriteOffAppVO colltWriteOffAppVO){
        AccLoanVO accLoanVO = accLoanService.queryLSAndDNByPK(colltWriteOffAppVO.getLoanNo());
        String loanNo = accLoanVO.getLoanSeq();
        String distrNo = accLoanVO.getDistrNo();
        String rsn = colltWriteOffAppVO.getWriteOffReason();
        AccountingReqBean accountingReqBean = new AccountingReqBean();
        accountingReqBean.setLoanNo(loanNo);
        accountingReqBean.setDistrNo(distrNo);
        accountingReqBean.setCnclRsn(rsn);
        AccountingRespBean accountingRespBean = new AccountingRespBean();
        try {
            accountingRespBean = accountingService.applyAccounting(accountingReqBean);
            if(accountingRespBean == null){
                logger.info("核销流水号为【" + colltWriteOffAppVO.getBizSerno() + "】，借据编号【" + colltWriteOffAppVO.getLoanNo() + "】调用互金核销接口失败");
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("核销流水号为【" + colltWriteOffAppVO.getBizSerno() + "】，借据编号【" + colltWriteOffAppVO.getLoanNo() + "】调用互金核销接口失败" +e.getMessage());
            return false;
        }
        return true;
    }

    @Autowired
    @Qualifier("accLoanService")
    private AccLoanService accLoanService;

}
